package httpHandler

import (
	"encoding/json"
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/http/service"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/model"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/model/req"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"reflect"
)

func CreateBanner(c *gin.Context) {
	var request req.BannerCreateReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	result, tmperr := json.Marshal(request.ReqImages)
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	request.Pictures = string(result)

	err := service.ServicePublicInstance.BannerCreate(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", nil)
}

func UpdateBanner(c *gin.Context) {
	var target map[string]interface{}
	if err := c.ShouldBindBodyWith(&target, binding.JSON); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	result, tmperr := json.Marshal(target["req_images"])
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	target["pictures"] = string(result)

	var request req.BannerUpdateReq
	elem := reflect.TypeOf(&request).Elem() //通过反射获取type定义
	for i := 0; i < elem.NumField(); i++ {
		if elem.Field(i).Tag.Get("gorm") == "-" && len(elem.Field(i).Tag.Get("json")) > 0 {
			delete(target, elem.Field(i).Tag.Get("json"))
		}
	}

	err := service.ServicePublicInstance.BannerUpdateForMap(target)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", nil)
}

func DeleteBannerList(c *gin.Context) {
	var request req.IdsReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.ServicePublicInstance.DeleteBannerList(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", nil)
}

func GetBannerList(c *gin.Context) {
	var request req.BannerListReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, total, err := service.ServicePublicInstance.GetBannerList(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"list":     list,
		"total":    total,
		"page":     request.Page,
		"pageSize": request.PageSize,
	})
}

func BannerList(c *gin.Context) {
	list, err := service.ServicePublicInstance.BannerList()
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"list": list,
	})
}

func GetWXACodeURL(c *gin.Context) {
	var request model.GenWXACodeURLReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"url": "test",
	})
}
